<template>
  <div>
    工资
    <h1>使用云储存</h1>
    <input type="file" @change="changeEvent">
    <img :src="imgSrc" alt="">
    <hr>
    <h3>使用upload上传</h3>
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="httprequest"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDlNkqfDUVFhsnQmqeBRQ7ZkeIfla9SUGl',
  SecretKey: 'FEvLUO1XmkRrzqQknZ5JGwMWP1LOAftr'
})
export default {
  data() {
    return {
      imgSrc: '',
      imageUrl: ''
    }
  },
  methods: {
    changeEvent(e) {
      console.log(e.target.files[0])
      const fileN = e.target.files[0]
      // 使用上传方法
      cos.putObject(
        {
          Bucket: 'dhf-80-1301625390' /* 填入您自己的存储桶，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，例如ap-beijing，必须字段 */,
          Key: '1.jpg' /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
          StorageClass: 'STANDARD',
          Body: fileN.name, // 上传文件对象
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          this.imgSrc = 'http://' + data.Location
          console.log(err || data)
        }
      )
    },
    // upload
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      console.log(file)
      const isJPG = file.type === 'image/jpeg' || 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 自定义上传行为
    httprequest(res) {
      console.log('httprequest', res)
      const fileN = res.file
      // 使用上传方法
      cos.putObject(
        {
          Bucket: 'dhf-80-1301625390' /* 填入您自己的存储桶，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，例如ap-beijing，必须字段 */,
          Key: '1.jpg' /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */,
          StorageClass: 'STANDARD',
          Body: fileN.name, // 上传文件对象
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          this.imageUrl = 'http://' + data.Location
          console.log(err || data)
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
